package com.alkalinesolutions.json.core.impl;

import java.util.List;
import java.util.Map;

import com.alkalinesolutions.json.core.Messages;
import com.alkalinesolutions.json.core.UnsupportedTypeException;

/**
 * Simple class to wrap a java object and hide custom equality/hash
 * mechanisms. For our cycle detection code, we merely want to make sure the
 * exact same object instance is not being added, not that a structurally
 * matching object is being added.
 * 
 * @author David Waite <david@alkaline-solutions.com>
 * 
 * @param <T>
 *            Value type contained within
 */
public enum JSONType {
	Null, Object, Boolean, String, Array, List, Number;

	public static JSONType valueFromObject(final Object data) throws UnsupportedTypeException {
		if (data == null)
			return JSONType.Null;
		if (data instanceof String)
			return JSONType.String;
		if (data instanceof Boolean)
			return JSONType.Boolean;
		if (data instanceof Map)
			return JSONType.Object;
		if (data instanceof List)
			return JSONType.List;
		if (data.getClass().isArray())
			return JSONType.Array;
		if (data instanceof Number)
			return JSONType.Number;

		throw new UnsupportedTypeException(java.lang.String.format(Messages.getString("Type.unknown-type"), data.getClass().getName())); //$NON-NLS-1$
	}
}